package com.test.jmx.agent;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;

import com.sun.jdmk.comm.HtmlAdaptorServer;

public class HelloAgent implements NotificationListener {

	private MBeanServer mbs = null;
	private final static String AGENT_NAME = "HelloAgent";

	public HelloAgent() {
		mbs = MBeanServerFactory.createMBeanServer(AGENT_NAME);
		HtmlAdaptorServer htmlAdaptor = new HtmlAdaptorServer();

		HelloWorld hw = new HelloWorld();

		ObjectName adaptorName = null;
		ObjectName helloWorldName = null;

		try {
			helloWorldName = new ObjectName("HelloAgent:name=helloWorld");
			mbs.registerMBean(hw, helloWorldName);
			hw.addNotificationListener(this, null, null);

			adaptorName = new ObjectName("HelloAgent:name=htmladapter,port=9092");
			htmlAdaptor.setPort(9092);

			mbs.registerMBean(htmlAdaptor, adaptorName);

			htmlAdaptor.start();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void handleNotification(Notification notif, Object handback) {
		System.out.println("Receiving notification...");
		System.out.println(notif.getType());
		System.out.println(notif.getMessage());
	}

	public static void main(String[] args) {
		System.out.println("HelloAgent is running");
		HelloAgent agent = new HelloAgent();
	}

}
